var is_tutor = 1;
var opt = '';
var courseNumber = 0;
var uId = getCookie('UNumber');
$.ajax({
    url : "http://120.79.245.60:8080/eooker/a/sys/userAPI/course",
    type : "get",
    dataType : "json",
    success:function(data){
        // console.log(data.resultMap.courses);
        $("#courseName").empty();
        $("#courseName").selectpicker({});
        opt = '<option>请选择课程信息</option>'
        $.each(data.resultMap.courses,function(i,o){
            opt+='<option>'+o+'</option>';
            // $(".selectpicker").append(opt);
        });
        $('#courseName').html(opt);
        $("#courseName").selectpicker('refresh');//动态加载
        $("#courseName").selectpicker('render');
    },
    error:function(){
        mesLay("课程信息为空请手动输入~");
    }
});
$('#courseLoct').selectpicker({});


$('.creatHouse').on('click',function(){

    var courseName = $('#courseName option:selected').text();
    var courseLct = $('#couselct').val();
    var reg = /^[ABab]\d-\d{3}$/;
    var csNameCode = encodeURIComponent(courseName).replace(/%20/gi, '').replace(/(!)|(')|(\()|(\))|(\~)/gi, item => {
        return '%' + item.charCodeAt(0).toString(16).toLocaleUpperCase();
    });
    if(courseName != '请选择课程信息' && reg.test(courseLct)) {
        var url = 'http://120.79.245.60:8080/eooker/a/sys/userAPI/room_number/' + courseLct + '/' + uId + '/' + csNameCode;
        $.ajax({
            url: url,
            type: 'Get',
            dataType: 'JSON',
            crossDomain: true,
            success: function (data) {

                if (data.message == 'Request Success') {
                    $('.hNumber').css('display', 'inline-block');
                    courseNumber = data.resultMap.roomNumber;
                    window.localStorage.setItem('CourseNumber', courseNumber);
                    $('.goHouse').css('display','block');
                    $('.creatHouse').css('display','none');
                    $('.hNumber-Tip').html('房间号为：' + courseNumber);
                }
            },
            fail: function (data) {
                mesLay("生成房间失败，请联系管理员");
            }
        });
    }else {
        mesLay('信息填写不到位呀~检查检查再创建！');
    }
});
$('.goHouse').on('click',function(){

    self.location = 'choose_seat.html';
});

function phone(){
    var reg = /^201\d{9}$/;
    var phoneVal = $('.tel').val();
    if(reg.test(phoneVal)){

    }else{
        mesLay('学号输入有误！');
        $('.tel').focus();
        return false;
    }
}

function couselct(){
    // 正确标记
    var reg = /^[ABab]\d-\d{3}$/;
    var couselct = $('.couselct').val();
    if(reg.test(couselct)){
        $(".fa-map-marker").css('color','#5cff19');
    }else{
        $(".fa-map-marker").css('color','black');
        mesLay('您输入的課室位置格式有误请重新输入！');
        return false;
    }
}

function courseName() {
    var courseName = $('#courseName option:selected').text();
    if( courseName == '请选择课程信息') $(".fa-home").css('color','black');
    else $(".fa-home").css('color','#5cff19');
}

function mesLay(content){
    layer.open({
        content: content
        ,skin: 'msg'
        ,time: 3
    });
}

$('.tel').blur(function(){
    phone();
});

$('.couselct').blur(function(){
    couselct();
});


$('.course').on('click',function(){
    courseName();
});
